using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using Model;
using Utility;

/// <summary>
/// Summary description for CommentService
/// </summary>
public class CommentService
{
    public int CreateComment(Comment comment)
    {
        MySqlParameter[] parms = new MySqlParameter[] {
                new MySqlParameter("?_Content",comment.Content),
                new MySqlParameter("?_CreatedBy",comment.CreatedBy),
                new MySqlParameter("?_CreatedTime",comment.CreatedTime),
                new MySqlParameter("?_IID",comment.IID)
            };

        return MySQLHelper.ExecuteNonQuery("sp_CreateComment", parms);
    }

    public IList<Comment> GetCommentListByIID(int IID)
    {
        IList<Comment> list = new List<Comment>();
        MySqlParameter parm = new MySqlParameter("?_IID", IID);
        using (MySqlDataReader sdr = MySQLHelper.ExecuteReader("sp_GetCommentListByIID", parm))
        {
            while (sdr.Read())
            {
                Comment com = new Comment();
                com.CID = int.Parse(sdr["CID"].ToString());
                com.IID = IID;
                com.Content = sdr["Content"].ToString();
                com.CreatedBy = sdr["CreatedBy"].ToString();
                com.CreatedTime = sdr["CreatedTime"].ToString();
                com.FName = sdr["FirstName"].ToString();
                com.LName = sdr["LastName"].ToString();

                list.Add(com);
            }
        }

        return list;
    }

    public IList<Comment> GetCommentListByIID(int IID, int PageSize, int PageIndex)
    {
        IList<Comment> list = new List<Comment>();
        MySqlParameter[] parms = new MySqlParameter[] {
                new MySqlParameter("?IdeaID", IID),
                new MySqlParameter("?PageSize",PageSize),
                new MySqlParameter("?PageIndex",PageIndex)
        };
        using (MySqlDataReader sdr = MySQLHelper.ExecuteReader("sp_GetCommentsByIdea", parms))
        {
            while (sdr.Read())
            {
                Comment com = new Comment();
                com.CID = int.Parse(sdr["CID"].ToString());
                com.IID = IID;
                com.Content = sdr["Content"].ToString();
                com.CreatedBy = sdr["CreatedBy"].ToString();
                com.CreatedTime = sdr["CreatedTime"].ToString();

                list.Add(com);
            }
        }

        return list;
    }

    public int GetCommentListCountByIID(int IID)
    {
        MySqlParameter parm = new MySqlParameter("?IdeaID", IID);
        return Convert.ToInt32(MySQLHelper.ExecuteScalar("sp_GetCommentsByIdea", parm));
    }
}
